#include <SharpShooter.h>
#include "TankBase.h"

CTankBase::CTankBase(float pX,float pY,CMeshSceneNode *meshNode, TANK_DIRECTION direction
                     ,int velocity):
m_Direction(direction),m_Velocity(velocity),
m_PositionX(pX),m_PositionY(pY),m_MeshNode(meshNode)
{	
	loadMap("../../Media/mapFile.txt");
}


void CTankBase::movement(TANK_DIRECTION direction)
{
	switch(direction)
	{
	case TANK_DIR_FORWARD:
		if (direction != m_Direction)
		{
			m_Direction = direction;
			break;
		}
		if (!aabBox(direction))
		{
			break;
		}
		m_PositionY -= m_Velocity * LENGTH_EACH_STEP;
		break;
	case TANK_DIR_BACKWARD:
		if (direction != m_Direction)
		{
			m_Direction = direction;
			break;
		}
		if (!aabBox(direction))
		{
			break;
		}
		m_PositionY += m_Velocity  * LENGTH_EACH_STEP;
		break;
	case TANK_DIR_LEFT:
		if (direction != m_Direction)
		{
			m_Direction = direction;
			break;
		}
		if (!aabBox(direction))
		{
			break;
		}
		m_PositionX += m_Velocity  * LENGTH_EACH_STEP;
		break;
	case TANK_DIR_RIGHT:
		if (direction != m_Direction)
		{
			m_Direction = direction;
			break;
		}
		if (!aabBox(direction))
		{
			break;
		}
		m_PositionX -= m_Velocity  * LENGTH_EACH_STEP;
		break;
	default:
		break;
	}
}
void CTankBase::update(void)
{
  Vector3f nowPosition;
  switch(m_Direction)
  {
  case TANK_DIR_FORWARD:		
    m_MeshNode->setRotation(Vector3f(0.0f,0.0f,0.0f));
    break;
  case TANK_DIR_BACKWARD:		
    m_MeshNode->setRotation(Vector3f(0.0f,MathLib::PI,0.0f));		
    break;
  case TANK_DIR_LEFT:
    m_MeshNode->setRotation(Vector3f(0.0f,-MathLib::PI/2,0.0f));
    break;
  case TANK_DIR_RIGHT:
    m_MeshNode->setRotation(Vector3f(0.0f,MathLib::PI/2,0.0f));
    break;
  default:
    break;
  }
  m_MeshNode->setPosition(Vector3f(static_cast<float>(m_PositionX),
    5.0f,static_cast<float>(m_PositionY)));
}
void CTankBase::setPosition()
{
  m_MeshNode->setPosition(Vector3f(static_cast<float>(m_PositionX ),
  0.0f,static_cast<float>(m_PositionY)));
}
int CTankBase::getPositionX()const
{
  return m_PositionX;
}

int CTankBase::getPositionY()const
{
  return m_PositionY;
}
void CTankBase::setVisible(bool visible)
{
  m_MeshNode->setVisible(visible);
}

bool CTankBase::aabBox(TANK_DIRECTION direction)
{
	int i;
	int j;
	j = m_PositionX/2.5-1;
	i = m_PositionY/2.5;
	
	switch (direction)
	{
	case TANK_DIR_FORWARD:
		if (i == 0)
		{
			return false;
		}
		if ((m_TerrainMap[i-1][j] != 0 && m_TerrainMap[i-1][j] != 3)||
			(m_TerrainMap[i-1][j+1] != 0 && m_TerrainMap[i-1][j+1] != 3)||
			(m_TerrainMap[i-1][j+2] != 0 && m_TerrainMap[i-1][j+2] != 3)||
			(m_TerrainMap[i-1][j+3] != 0 && m_TerrainMap[i-1][j+3] != 3))
		{
			return false;
		}
		break;
	case TANK_DIR_BACKWARD:	
		if (i == 56)
		{
			return false;
		}
		if ((m_TerrainMap[i+4][j] != 0 && m_TerrainMap[i+4][j] != 3)||
			(m_TerrainMap[i+4][j+1] != 0 && m_TerrainMap[i+4][j+1] != 3)||
			(m_TerrainMap[i+4][j+2] != 0 && m_TerrainMap[i+4][j+2] != 3)||
			(m_TerrainMap[i+4][j+3] != 0 && m_TerrainMap[i+4][j+3] != 3))
		{
			return false;
		}
		break;
	case TANK_DIR_LEFT:
		if (j == 72)
		{
			return false;
		}
		if ((m_TerrainMap[i][j+4] != 0 && m_TerrainMap[i][j+4] != 3)||
			(m_TerrainMap[i+1][j+4] != 0 && m_TerrainMap[i+1][j+4] != 3)||
			(m_TerrainMap[i+2][j+4] != 0 && m_TerrainMap[i+2][j+4] != 3)||
			(m_TerrainMap[i+3][j+4] != 0 && m_TerrainMap[i+3][j+4] != 3))
		{
			return false;
		}
		break;
	case TANK_DIR_RIGHT:
		if (j == 0)
		{
			return false;
		}
		if ((m_TerrainMap[i][j-1] != 0 && m_TerrainMap[i][j-1] != 3)||
			(m_TerrainMap[i+1][j-1] != 0 && m_TerrainMap[i+1][j-1] != 3)||
			(m_TerrainMap[i+2][j-1] != 0 && m_TerrainMap[i+2][j-1] != 3)||
			(m_TerrainMap[i+3][j-1] != 0 && m_TerrainMap[i+3][j-1] != 3))
		{
			return false;
		}
		break;
	default:
		break;
		return true;
	}
}

void CTankBase::loadMap(const char *mapFile)
{
	cout << m_PositionX/2.5 << endl << m_PositionY/2.5;
	cout<<"l_Map LoadIng..."<<endl;
	string str;
	string substr;
	ifstream file(mapFile);
	int i = 0;
	int j = 0;
	while (getline(file, str))
	{
		string::size_type begin = 0;
		string::size_type end = 0;
		begin = str.find_first_not_of(" ",begin);
		while (begin != string::npos)
		{
			end = str.find_first_of(" ",begin);
			if(end == std::string::npos)
			{
				substr = str.substr(begin);
				m_TerrainMap[i][j] =atoi(substr.c_str());				
				++j;
				break;			
			}
			substr = str.substr(begin,end-begin);
			m_TerrainMap[i][j] = atoi(substr.c_str());	
			++j;
			begin = str.find_first_not_of(" ",end);
		}
		j = 0;	
		++i;
	}
}